﻿using Dapper;
using System;
using BCEntity.TMS.Order.OrderMoveHouseSpecificationServiceRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.Order.OrderMoveHouseSpecificationServiceRecord
{
	/// <summary>
	/// 订单搬家特殊服务记录
	/// </summary>
	public class OrderMoveHouseSpecificationServiceRecordData : IOrderMoveHouseSpecificationServiceRecordData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM ordermovehousespecificationservicerecord";
		private const string QUERY = @"SELECT OrderMoveHouseSpecificationServiceRecordId,Price,CreateTime,OrderId,OrderNo,MoveHouseSpecificationServiceConfigId,MoveHouseConfigId,
											MoveHouseSpecificationConfigId,SpecificationName,SpecificationServiceConfigId,SpecificationServiceName 
										FROM ordermovehousespecificationservicerecord";
		public OrderMoveHouseSpecificationServiceRecordData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查订单搬家特殊服务记录是否存在
		public async Task<bool> Exists(long orderMoveHouseSpecificationServiceRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderMoveHouseSpecificationServiceRecordId=?OrderMoveHouseSpecificationServiceRecordId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderMoveHouseSpecificationServiceRecordId", orderMoveHouseSpecificationServiceRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加订单搬家特殊服务记录
		public async Task<OrderMoveHouseSpecificationServiceRecordEntity> Add(OrderMoveHouseSpecificationServiceRecordEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("ordermovehousespecificationservicerecord", new string[] { "OrderId", "OrderNo", "MoveHouseSpecificationServiceConfigId",
				"MoveHouseConfigId", "MoveHouseSpecificationConfigId", "SpecificationName", "SpecificationServiceConfigId", "SpecificationServiceName","Price" });
			sql = string.Format("{0} {1} WHERE OrderMoveHouseSpecificationServiceRecordId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?MoveHouseSpecificationServiceConfigId", entity.MoveHouseSpecificationServiceConfigId, DbType.Int64, size: 20);
			parameters.Add("?MoveHouseConfigId", entity.MoveHouseConfigId, DbType.Int64, size: 20);
			parameters.Add("?MoveHouseSpecificationConfigId", entity.MoveHouseSpecificationConfigId, DbType.Int64, size: 20);
			parameters.Add("?SpecificationName", entity.SpecificationName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SpecificationServiceConfigId", entity.SpecificationServiceConfigId, DbType.Int32, size: 11);
			parameters.Add("?SpecificationServiceName", entity.SpecificationServiceName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?Price", entity.Price, DbType.Decimal);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderMoveHouseSpecificationServiceRecordEntity>(sql, parameters, databaseContext.Transaction);

		}
		public async Task<bool> Add(List<OrderMoveHouseSpecificationServiceRecordEntity> entities)
        {
			return await this.databaseContext.Master.InsertAllAsync(entities, databaseContext.Transaction) > 0;
        }
		#endregion

		#region 修改订单搬家特殊服务记录
		public async Task<OrderMoveHouseSpecificationServiceRecordEntity> Update(OrderMoveHouseSpecificationServiceRecordEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("ordermovehousespecificationservicerecord", new string[] { "Price", "OrderId", "OrderNo", "MoveHouseSpecificationServiceConfigId",
				"MoveHouseConfigId", "MoveHouseSpecificationConfigId", "SpecificationName", "SpecificationServiceConfigId", "SpecificationServiceName" },
				new string[] { "OrderMoveHouseSpecificationServiceRecordId" });
			sql = string.Format("{0} {1} WHERE OrderMoveHouseSpecificationServiceRecordId=?OrderMoveHouseSpecificationServiceRecordId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderMoveHouseSpecificationServiceRecordId", entity.OrderMoveHouseSpecificationServiceRecordId, DbType.Int64, size: 20);
			parameters.Add("?Price", entity.Price, DbType.Decimal, size: 10);
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?MoveHouseSpecificationServiceConfigId", entity.MoveHouseSpecificationServiceConfigId, DbType.Int64, size: 20);
			parameters.Add("?MoveHouseConfigId", entity.MoveHouseConfigId, DbType.Int64, size: 20);
			parameters.Add("?MoveHouseSpecificationConfigId", entity.MoveHouseSpecificationConfigId, DbType.Int64, size: 20);
			parameters.Add("?SpecificationName", entity.SpecificationName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SpecificationServiceConfigId", entity.SpecificationServiceConfigId, DbType.Int32, size: 11);
			parameters.Add("?SpecificationServiceName", entity.SpecificationServiceName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderMoveHouseSpecificationServiceRecordEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除订单搬家特殊服务记录
		public async Task<bool> Delete(long orderMoveHouseSpecificationServiceRecordId)
		{
			string sql = @"DELETE FROM ordermovehousespecificationservicerecord WHERE OrderMoveHouseSpecificationServiceRecordId=?OrderMoveHouseSpecificationServiceRecordId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderMoveHouseSpecificationServiceRecordId", orderMoveHouseSpecificationServiceRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取订单搬家特殊服务记录
		public async Task<OrderMoveHouseSpecificationServiceRecordEntity> GetEntity(long orderMoveHouseSpecificationServiceRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderMoveHouseSpecificationServiceRecordId=?OrderMoveHouseSpecificationServiceRecordId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderMoveHouseSpecificationServiceRecordId", orderMoveHouseSpecificationServiceRecordId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderMoveHouseSpecificationServiceRecordEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单搬家特殊服务记录列表
		public async Task<IEnumerable<OrderMoveHouseSpecificationServiceRecordEntity>> GetList()
		{
			return await this.databaseContext.Master.QueryAsync<OrderMoveHouseSpecificationServiceRecordEntity>(QUERY);
		}
		#endregion

		#region 获取订单搬家特殊服务记录分页
		public async Task<(IEnumerable<OrderMoveHouseSpecificationServiceRecordEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM ordermovehousespecificationservicerecord {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<OrderMoveHouseSpecificationServiceRecordEntity, int>(sql, parameters);
		}
		#endregion
	}
}



